/**
 * 历史记录请求
 * */
const {
    express,
    db_mysql,
    errorData,
    enCodePWD,
    strLen
} = require('./common-req.js')
const router = express.Router();
const { StringDecoder } = require('string_decoder');
const decoder = new StringDecoder('utf8');

router.post('/', (req, res) => {
    const phone = req.body.phone;
    const botanyId = req.body.botanyId;
    const historyId = strLen();
    const time = new Date();
    
    let insertSql = 'insert into historys(historyId, botanyId, phone, time) values(?, ?, ?, ?)';
    let insertArr = [historyId, botanyId, phone, time];
    let resData = {
        data: null,
        status: 0,
        msg: '记录成功'
    }
    db_mysql.insert(insertSql, insertArr, result => {
        if(result) {
            res.send(resData)
        } else {
            errorData.msg = '记录失败';
            errorData.code = 200
            res.send(errorData)
        }
    })
})

router.get('/', (req, res) => {
    const phone = req.query.phone;
    const pageNum = parseInt(req.query.pageNum) - 1 >= 0 ? parseInt(req.query.pageNum) - 1 : 0;
    const pageSize = parseInt(req.query.pageSize);
    let countSql = 'select count(*) as count from historys where phone=?';
    let selectSql = 'select * from historyview where phone=? limit ?,?';
    let selectArr = [phone, pageNum*pageSize, pageSize]
    let resData = {
        data: [],
        status: 0,
        msg: '获取记录成功',
        total: 0
    }
    db_mysql.select(countSql, phone, countRes => {
        console.log(countRes, phone, selectArr)
        if(countRes && countRes.length > 0) {
            resData.total = countRes[0].count
        }
        db_mysql.select(selectSql, selectArr, result => {
            result.forEach(item => {
                let base = decoder.write(item.baseInfo);
                let grow = decoder.write(item.growInfo);
                let form = decoder.write(item.formInfo);
                let cul = decoder.write(item.cultivateInfo);
                var obj = {
                    historyId: item.historyId,
                    botanyId: item.botanyId,
                    phone: item.hone,
                    time: item.time,
                    botanyId: item.botanyId,
                    doorId: item.doorId,
                    outlineId: item.outlineId,
                    sectionId: item.sectionId,
                    genusId: item.genusId,
                    name: item.name,
                    baseInfo: base === '' ? base : JSON.parse(base),
                    growInfo: grow === '' ? grow : JSON.parse(grow),
                    formInfo: form === '' ? form : JSON.parse(form),
                    cultivateInfo: cul === '' ? cul : JSON.parse(cul),
                    sectionId: item.sectionId,
                    src: item.src,
                }
                resData.data.push(obj)
            })
            res.send(resData)
        })
    })
    
})

router.get('/del', (req, res) => {
    const historyId = JSON.parse(req.query.historyId);
    let delSql = 'delete from historys where historyId=?';
    let len = historyId.length - 1;
    historyId.forEach((item, index) => {
        db_mysql.del(delSql, item, result => {
            console.log('删除')
        })
        if(len === index) {
            res.send({
                data: [],
                status: 0,
                msg: '删除记录成功'
            })
        }
    })
    
})

module.exports = router;